/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package tetris.model;

import java.util.ArrayList;
import java.util.Random;
import tetris.model.navigation.Direction;
import tetris.model.navigation.Position;

/**
 * Класс составной фигуры.
 * @author vadim
 */
public class CompositeFigure extends Figure {
    private Figure _figure1;
    private Figure _figure2;
    
    /**
     * Конструктор
     */
    public CompositeFigure() {
        Random rand = new java.util.Random();
        int number = rand.nextInt(7);
        _figure1 = Judge.getFigureByNumber(number);
        number = rand.nextInt(7);
        _figure2 = Judge.getFigureByNumber(number);    
        int minWidth1 = _figure1.form().get(0).length, minWidth2 = _figure1.form().get(0).length;
        int maxWidth1 = 0, maxWidth2 = 0; 
        
        for (int i = 0; i < _figure1.form().size(); i++) {
            for (int j = 0; j < _figure1.form().get(i).length; j++) {
                if (_figure1.form().get(i)[j]) {
                    if (j > maxWidth1) {
                        maxWidth1 = j;
                    }
                    if (j < minWidth1) {
                        minWidth1 = j;
                    }                    
                }                
            }
        }
        
        for (int i = 0; i < _figure2.form().size(); i++) {
            for (int j = 0; j < _figure2.form().get(i).length; j++) {
                if (_figure2.form().get(i)[j]) {
                    if (j > maxWidth2) {
                        maxWidth2 = j;
                    }
                    if (j < minWidth2) {
                        minWidth2 = j;
                    }                    
                }                
            }
        }
        
        _form = new ArrayList<boolean[]>();
        int length = maxWidth1 + maxWidth2 + 2 - minWidth2 - minWidth1;
        boolean[] row = new boolean[length];
        for (int i = 0; i < _figure1.form().size(); i++ ) {
            for (int j = 0; j < length; j++) {
                row[j] = false;
            }
            
            for (int j = 0; j < length; j++) {
                if (j < maxWidth1 - minWidth1+1)
                    row[j] = _figure1.form().get(i)[minWidth1 + j];
                else
                    row[j] = _figure2.form().get(i)[j - maxWidth1 + minWidth2 + minWidth1-1];
            }
            
            _form.add(row.clone());
        } 
    }
    
    private void disintegrate() {
        int center1 = _figure1.form().get(0).length/2;
        int center2 = _figure2.form().get(0).length/2;
        int minWidth2 = _figure1.form().get(0).length;
        int maxWidth1 = 0; 
        
        for (int i = 0; i < _figure2.form().size(); i++) {
            for (int j = 0; j < _figure2.form().get(i).length; j++) {
                if (_figure2.form().get(i)[j] && j < minWidth2) {
                    minWidth2 = j;                   
                }                
            }
        }
        
        for (int i = 0; i < _figure1.form().size(); i++) {
            for (int j = 0; j < _figure1.form().get(i).length; j++) {
                if (_figure1.form().get(i)[j] && j > maxWidth1) {
                    maxWidth1 = j;                   
                }                
            }
        }
        
        Figure figure1,figure2;
        figure1 = (Figure)_figure1.clone();
        figure2 = (Figure)_figure2.clone();
        figure1._glass = _glass;
        figure2._glass = _glass;
        figure1._position = new Position(_position.row(), 
                                                  _position.column() - maxWidth1 + center1 - 1);
        figure2._position = new Position(_position.row(), 
                                                  _position.column() + center1 - minWidth2);
        int i = 0;
        while (_glass.crossing(figure2)) {
            i++;
            figure1._position = new Position(_position.row(), 
                                                  _position.column() - maxWidth1 + center1 - 1 - i);
            figure2._position = new Position(_position.row(), 
                                                  _position.column() + center1 - minWidth2 - i);
        }
        i = 0;
        while (_glass.crossing(figure1)) {
            i++;
            figure1._position = new Position(_position.row(), 
                                                  _position.column() - maxWidth1 + center1 - 1 + i);
            figure2._position = new Position(_position.row(), 
                                                  _position.column() + center1 - minWidth2 + i);
        }
        _figure1._listeners = _listeners;
        _figure2._listeners = _listeners;
        Random rand = new java.util.Random();
        int number = rand.nextInt(2);
        if (number == 1) {
            _glass.setCurrentFigure(_figure1);
            _glass.addSecondFigure(_figure2);
        } else {
            _glass.setCurrentFigure(_figure2);
            _glass.addSecondFigure(_figure1);     
        }
        _figure1.setPosition(_glass, figure1.position());
        _figure2.setPosition(_glass, figure2.position());
    }
    
    /**
     * Фунция проверки возможности поворота.
     * @return Результат проверки.
     */
    @Override
    public boolean turnIsPossible() {
        return false;
    }
    
    /**
     * Функция реализующая движение фигуры.
     * @param direct направление движения.
     * @return Результат движения.
     */
    @Override
    public boolean move(Direction direct) {
        boolean result = super.move(direct);
        if(result && _glass.height() - 4 == _position.row()) {
            disintegrate();    
        }
        return result;
    }
}
